package de.koller.gravity.mech.particle.creator;

import de.koller.aframe.util.misc.ARangeD;
import de.koller.gravity.mech.particle.Particle;
import de.koller.gravity.mech.particle.ParticleEngine;
import de.koller.oceanFramework.util.ColorIntUtil;
import java.util.HashSet;
import java.util.Set;

public class ParticleCannon implements IParticleCreator {
	private final Set<Particle> particles = new HashSet<>();
	private double paticlesToGen = 0;
	
	public final ARangeD shootVelocity = new ARangeD( 0, 0 );
	public final ARangeD shootDirection = new ARangeD( 0, 0 );
	public final ARangeD particlesPerStep = new ARangeD( 1, 1 );
	
	
	@Override
	public Set<Particle> creationStep( ParticleEngine engine ) {
		particles.clear();

		paticlesToGen += particlesPerStep.getRandomValue( engine.rnd );
		int toCreate = (int)Math.floor( paticlesToGen );
		
		for( int i = 0; i < toCreate; i++ )
			particles.add( create( engine ) );
		
		paticlesToGen -= toCreate;
		return particles;
	}
	
	private Particle create( ParticleEngine engine ) {
		Particle p = new Particle();
		p.velocity.setTo2D( shootDirection.getRandomValue( engine.rnd ), 
							shootVelocity.getRandomValue( engine.rnd ) );
		p.color = ColorIntUtil.getIntColor( 0xff,	engine.rnd.nextInt( 0xff ),
													engine.rnd.nextInt( 0xff ),
													engine.rnd.nextInt( 0xff ) );
		
		return p;
	}
	
}
